package com.bingxue.edu.teacher.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.management.auth.EmployeeAuthUtil;
import com.bingxue.edu.management.education.model.entity.EnrollChangeRecord;
import com.bingxue.edu.teacher.model.query.TeacherClassChangeQuery;
import com.bingxue.edu.teacher.model.query.TeacherClassInfoQuery;
import com.bingxue.edu.teacher.model.query.TeacherClassStudentQuery;
import com.bingxue.edu.teacher.model.resp.TeacherClassInfoResp;
import com.bingxue.edu.teacher.model.resp.TeacherClassStudentResp;
import com.bingxue.edu.teacher.service.TeacherClassInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * 教师端小程序班级模块
 * @author zq
 * @since 2024/10/13
 */
@Tag(name = "教师端-班级模块")
@RestController
@RequestMapping("/t/class")
@RequiredArgsConstructor
public class TeacherClassInfoController {

    private final TeacherClassInfoService teacherClassInfoService;

    @Operation(summary = "分页查询班级列表")
    @GetMapping("/page")
    public Response<Pagination<TeacherClassInfoResp>> getClassInfoPage(TeacherClassInfoQuery query, @Validated PageQuery pageQuery) {
        String currentUserId = EmployeeAuthUtil.getCurrentUserId();
        if (EmployeeAuthUtil.isAssistTeacher()) {
            query.setAssistTeacherId(currentUserId);
        } else {
            query.setLeadTeacherId(currentUserId);
        }
        return Response.success(teacherClassInfoService.getClassInfoPage(query, pageQuery));
    }

    @Operation(summary = "查询班级详情")
    @GetMapping("/detail/{id}")
    public Response<TeacherClassInfoResp> getDetailById(@PathVariable String id) {
        return Response.success(teacherClassInfoService.getDetailById(id));
    }

    @Operation(summary = "查询班级学生列表")
    @GetMapping("/student/list")
    public Response<List<TeacherClassStudentResp>> getStudentList(TeacherClassStudentQuery query) {
        return Response.success(teacherClassInfoService.getClassStudentList(query));
    }

    @Operation(summary = "查询近期班级变更记录")
    @GetMapping("/list-change-record")
    public Response<List<EnrollChangeRecord>> getRecentChangeRecordList(TeacherClassChangeQuery query) {
        String currentUserId = EmployeeAuthUtil.getCurrentUserId();
        if (EmployeeAuthUtil.isAssistTeacher()) {
            query.setAssistTeacherId(currentUserId);
        } else {
            query.setLeadTeacherId(currentUserId);
        }
        if (query.getStartDate() == null) {
            query.setStartDate(LocalDate.now());
        }
        return Response.success(teacherClassInfoService.getRecentChangeRecordList(query));
    }

}
